<?php

namespace Desktopd\MUA;

// POP3 (Mail Receiver)
class POP3Client {
    const NO_SECURITY = 0;
    const TLS_PORT = 1; // TLS-only port
    const SMTPS = self::TLS_PORT;
    const STARTTLS = 2; // no dedicated TLS port, but STARTTLS is required
    
    const TLS_ALLOW_INSECURE = 4; // should be used with certificate pinning
    
    
    protected $host;
    protected $type;
    protected $user;
    protected $password;
    
    protected $pinnedCertificate = false;
    protected $ca = false;
    protected $ciphers = false;
    protected $PFS = false;
    
    
    public function __construct ($host, $type = 0, $user, $password, $port = null) {
        $this->host = $host;
        $this->type = $type;
        
        $this->user = $user;
        $this->password = $password;
        
        if ($port && is_numeric($port) && $port > 0 && $port < 65536) {
            $this->port = (string) round($port);
        } elseif ($this->isTLSPort()) {
            $this->port = '995';
        } else {
            $this->port = '110';
        }
    }
    
    public function getHost () {
        return $this->host;
    }
    
    public function getPort () {
        return $this->port;
    }
    
    public function isTLSPort () {
        return (bool) (self::TLS_PORT & $this->type);
    }
    
    public function isPOPS () {
        return $this->isTLSPort();
    }
    
    public function isSTARTTLS () {
        return (bool) (self::STARTTLS & $this->type);
    }
    
    public function isInsecure () {
        return (bool) (self::TLS_ALLOW_INSECURE & $this->type);
    }
    
    public function enableCertificatePinning ($certificate) {
        if (!$certificate) return false;
        return $this->pinnedCertificate = $certificate;
    }
    
    public function addCA ($certificate) {
        if (!$certificate) return false;
        return $this->ca = $certificate;
    }
    
    public function setCiphers ($ciphers) {
        if (!$ciphers) return false;
        return $this->ciphers = $ciphers;
    }
    
    public function requirePFS () {
        $this->PFS = true;
    }
    
    public function isPFSRequired() {
        return $this->PFS;
    }
    
    public function getPinnedCertificate () {
        return $this->pinnedCertificate;
    }
    
    public function getCA () {
        return $this->ca;
    }
    
    public function getCiphers () {
        return $this->ciphers;
    }
    
    public function getUser () {
        return $this->user;
    }
    
    public function getPassword () {
        return $this->password;
    }
    
    public function createReceiver () {
        return new POP3Receiver($this);
    }
}


// vim: ts=4 et ai

